package com.bzchao;

import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;

import org.vosk.LogLevel;
import org.vosk.Recognizer;
import org.vosk.LibVosk;
import org.vosk.Model;

/**
 * 从指定目录加载模型和语音文件
 */
public class DecoderDemoC {

    public static void test(String[] argv) throws IOException, UnsupportedAudioFileException {
        LibVosk.setLogLevel(LogLevel.DEBUG);

        try {
            Model model = new Model("C:\\model\\vosk-model-small-cn-0.22");
            InputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream("C:\\model\\test_16k.wav")));
            Recognizer recognizer = new Recognizer(model, 16000);

            int bytes;
            byte[] b = new byte[4096];
            while ((bytes = ais.read(b)) >= 0) {
                recognizer.acceptWaveForm(b, bytes);
            }

            System.out.println(recognizer.getFinalResult() + System.lineSeparator());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}